package com.peter.design.pattern.flyweight.general;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: 肖冬
 * @Date: 2020/7/27 20:54
 * @Description: 享元工厂
 */
//这个例子差点意思 如果intrinsicState是内部状态，那么应该只缓存intrinsicState，不应该把外部对象也缓存
public class FlyweightFactory {
    private static Map<String,IFlyweight> pool = new HashMap<>();

    // 因为内部状态具备不变性，因此作为缓存的键
    public static IFlyweight getFlyweight(String intrinsicState){
        if (!pool.containsKey(intrinsicState)) {
            IFlyweight flyweight = new ConcreteFlyweight(intrinsicState);
            pool.put(intrinsicState, flyweight);
        }
        return pool.get(intrinsicState);
    }
}
